package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class CourseServiceIpml implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> list(CourseDto courseDto) {
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());
        List<Course> courseList = courseMapper.list(courseDto);
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById( id);
    }

    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.add( course);
    }

    @Override
    public Course getById(Integer id) {
        Course course = courseMapper.getById(id);
        return course;
    }

    @Override
    public void upDate(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.upDate( course);
    }

    @Override
    public List<Course> listAll() {
        List<Course> courseList = courseMapper.listAll();
        return courseList;
    }

    @Override
    public List<Course> listBySubject(Integer subject) {
      List<Course> courseList =  courseMapper.listBySubject( subject);
        return courseList;
    }

}
